import requests, re, csv

url = 'https://movie.douban.com/top250'

headers = {
    'user-agent' : 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) '
    'Chrome/138.0.0.0 Mobile Safari/537.36 Edg/138.0.0.0',

    'referer' : 'https://cn.bing.com/',

    'cookie' : 'viewed="7061934"; bid=U0yKajd6Qmk; _vwo_uuid_v2=D26CB713E8ED8AA5CA2D6A5382FEF2348|69fe6a6514d2b'
    'd97cc4a43ee9b3091c7; ll="108303"; _pk_id.100001.4cf6=16ed372bf5eea2ec.1754370242.;'
    ' __yadk_uid=QyHTg9W0Ksxoxb5ksPfQlT07Im1t2p20; _pk_ref.100001.4cf6=%5B%22%22%2C%22%22%2C1754889508%2C%22ht'
    'tps%3A%2F%2Fcn.bing.com%2F%22%5D; _pk_ses.100001.4cf6=1; ap_v=0,6.0; __utma=30149280.1438868181.1747582314'
    '.1754370214.1754889509.3; __utmb=30149280.0.10.1754889509; __utmc=30149280; __utmz=30149280.1754889509.3.3'
    '.utmcsr=cn.bing.com|utmccn=(referral)|utmcmd=referral|utmcct=/; __utma=223695111.1161663825.1754370242.175'
    '4370242.1754889509.2; __utmb=223695111.0.10.1754889509; __utmc=223695111; __utmz=223695111.1754889509.2.2.'
    'utmcsr=cn.bing.com|utmccn=(referral)|utmcmd=referral|utmcct=/; _ga_Y4GN1R87RG=GS2.1.s1754889734$o1$g0$t1754'
    '889734$j60$l0$h0; _ga=GA1.2.886118325.1754889734; _gid=GA1.2.1062971807.1754889735',

    'accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7'
}

resp = requests.get(url, headers=headers)
print(type(resp))
page_content = resp.text
print(type(page_content))

# with open('douban_top250.text', 'w', encoding='utf-8') as f:
#     # 将爬取的内容写入文件
#     f.write(page_content)

obj = re.compile(r'<li>.*?<span class="title">(?P<name>.*?)</span>'
                 r'.*?<br>(?P<date_year>.*?)&nbsp'
                 r'.*?<span class="rating_num" property="v:average">(?P<score>.*?)</span>'
                 r'.*?<span>(?P<number>.*?)人评价', re.S)
results = obj.finditer(page_content)



    
with open('data.csv', mode='w', encoding='utf-8') as f:
    csvwrite = csv.writer(f) #创建一个CSV写入器对象，用于将数据写入文件
    csvwrite.writerow(['电影名称', '上映年份', '评分', '评价人数'])#作用：将一个可迭代对象（如列表、元组）中的元素作为一行数据写入 CSV 文件，元素之间用逗号分隔。
    for res in results:
        dic = res.groupdict() #作用：将正则表达式匹配结果转换为字典，字典的键是正则表达式中命名捕获组的名称，值是对应的匹配内容。
        dic['date_year'] = dic['date_year'].strip()
        csvwrite.writerow(dic.values())
f.close()
resp.close()
